<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <div class="pub-table-action-btn">
          <excel ref="exportSelect" file-name="混凝土结算" is-click-generate="false">
            <a-button slot="excelBtn" @click="exportSelect"> 导出选中 </a-button>
          </excel>
          <excel ref="exportAll" file-name="混凝土结算" is-click-generate="false">
            <a-button slot="excelBtn" @click="exportAll"> 导出全部 </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon type="info-circle" theme="twoTone" />
          已选择{{ selectedRowKeys.length }}项
          <span style="padding/material/settlement/leaseleft: 10px">
            总计：{{ totalSize }}， 待提交审核：{{ statistics.draft }}， 审核中：{{ statistics.submitted }}， 审核通过：{{ statistics.past }}， 审核不通过：{{ statistics.rejected }}， 已作废：{{ statistics.delete }}
          </span>
        </div>
      </div> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :start-index="0"
        :out-selected-row-keys="selectedRowKeys"
        :export-permission="{ select: '', all: '' }"
        :statistics-config="{ use: true, statistics }"
        :export-config="{
          use: false,
          select: $route.meta.title,
          all: $route.meta.title
        }"
      >
        <a-button slot="prefix" v-btnPermission="'mm_014_add_01'" type="primary" icon="plus" @click="handleEdit(null, 1)"> 新增 </a-button>
        <template slot="index" slot-scope="text, record, index">
          {{ index + 1 }}
        </template>
        <template slot="auditStatusStr" slot-scope="text, record">
          <list-status :status="record.auditStatus" :text="text" />
        </template>
        <div slot="operation" slot-scope="text, record">
          <MoreButton>
          <a @click="handleEdit(record, 3)">查看</a>
          <template v-if="(record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED')&&hideBuquBtn()">
            <a @click="handleEdit(record, 2)">编辑</a>
            <a @click="handleDel(record.id)">删除</a>
          </template>

          <approval-button :id="record.id" :status="record.auditStatus"
                           v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' " />
          </MoreButton>
        </div>
      </list-table>
      <!-- <pagination v-model="pageNo" :total="totalSize" :page-size.sync="pageSize" @change="handlePageChange" @showSizeChange="showSizeChange" /> -->
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { concreteList, settlementDel, concreteFindListCount } from '@/api/material/settlement/settlement'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'

export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      loading: false,
      queryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchCompanyId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId'
        // },
        {
          name: '混凝土结算编码',
          value: 'settleNum',
          type: 'input'
        },
        {
          name: '合同名称',
          value: 'materialContractName',
          type: 'input'
        },
        {
          name: '合同编号',
          value: 'materialContractCode',
          type: 'input'
        },
        {
          name: '工程名称',
          value: 'constructionTypeId',
          type: 'select',
          data: [],
          request: () => selectOption({ type: 18 })
        },
        // {
        //     name: '我方单位',
        //     value: 'ownDepartment',
        //     type: 'input'
        // },
        {
          name: '供应商名称',
          value: 'supplier',
          type: 'input'
        },
        {
          name: '结算年月',
          value: 'settleMonth',
          type: 'month'
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data: this.STATUS
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        },
        {
          title: '所属分公司',
          dataIndex: 'branchCompany',
          width: 150
        },
        {
          title: '项目名称',
          dataIndex: 'project',
          width: 150
        },
        {
          title: '混凝土结算编码',
          dataIndex: 'code',
          width: 140
        },
        {
          title: '合同名称',
          dataIndex: 'materialContractName',
          width: 120
        },
        {
          title: '合同编号',
          dataIndex: 'materialContractCode',
          width: 120
        },
        {
          title: '工程名称',
          dataIndex: 'constructionTypeName',
          width: 120
        },
        // {
        //     title: '我方单位',
        //     dataIndex: 'ownDepartment',
        //     width: 120
        // },
        {
          title: '供应商名称',
          dataIndex: 'supplier',
          width: 120
        },
        {
          title: '结算年月',
          dataIndex: 'settleMonth',
          width: 120
        },
        {
          title: '状态',
          width: this.STATUS_WIDTH,
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 200,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      selectedRowKeys: [],
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      tableData: [],
      loadData: (params) => {
        let searchData = Object.assign(params, this.queryParam)
        concreteFindListCount(searchData).then((res) => {
          if (res.code === this.SUCCESS_CODE) this.statistics = res.data
        })
        return concreteList(searchData).then((res) => {
          return res
        })
      },
      statistics: {},
      pathName: this.$route.path
    }
  },
  created() {
    // this.list()
  },
  mounted: function () {
    // this.list()
    this.$refs.table.refresh()
    // this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
    // this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    console.log('mounted-----------table的Path', this.pathName)
    // 监听详情页返回刷新列表
    //this.pathName取的值是this.$route.path
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      console.log('--------------------收到了，刷新刷新刷新----------------', this.pathName)
      // this.list()
      this.$refs.table.refresh()
    })
  },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    exportSelect: function () {
      if (this.selectedRowKeys.length > 0) {
        this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
      } else {
        this.$message.warning('请选择需要导出的数据')
      }
    },
    exportAll: function () {
      // 参数：查询条件、导出的回调
      if (this.totalSize) {
        this.pageNo = 1
        this.list(false, this.$refs.exportAll.setDataAndGenerate)
      } else {
        this.$message.warning('没有可导出的数据')
      }
    },
    list(params, callback) {
      let _this = this
      this.loading = true
      let searchData = Object.assign(
        {
          pageSize: callback ? _this.totalSize : _this.pageSize,
          pageNo: _this.pageNo
        },
        params,
        this.queryParam
      )
      concreteFindListCount(searchData).then((res) => {
        if (res.code === this.SUCCESS_CODE) this.statistics = res.data
      })
      concreteList(searchData).then((res) => {
        if (res.code === 200) {
          if (callback) {
            if (res.data.length) {
              callback(res.data)
            } else {
              this.$message.warning('没有可导出的数据')
            }
            this.loading = false
            return
          }
          _this.tableData = res.data
          _this.totalSize = res.totalSize
        }
        this.loading = false
      })
    },
    search(val) {
      this.pageNo = 1
      if (val.settleMonth) {
        val.settleMonth = this.getNowTime(val.settleMonth, 'M')
      }
      this.queryParam = val
      // this.list(val)
      this.$refs.table.refresh()
    },
    // handlePageChange(pageNo, pageSize) {
    //   this.pageNo = pageNo
    //   this.list()
    //   this.selectedRowKeys = []
    // },
    // showSizeChange(pageNo, pageSize) {
    //   this.pageNo = 1
    //   this.pageSize = pageSize
    //   this.list()
    // },
    // onSelectChange(selectedRowKeys, selectedRows) {
    //   console.log('selectedRowKeys',selectedRowKeys)
    //   let _this = this
    //   _this.selectId = []
    //   selectedRows.map((item) => {
    //     _this.selectId.push(item.id)
    //   })
    //   this.chooseData = selectedRowKeys.length
    //   _this.selectedRowKeys = selectedRowKeys
    //   _this.selectedRows = selectedRows
    // },
    handleDel(id) {
      let params = id ? [id] : this.selectId
      this.$delList2(
        settlementDel,
        () => {
          this.search({})
          this.selectedId = []
          this.selectedRowKeys = []
          this.selectedRows = []
          this.editingKey = ''
        },
        { ids: params }
      )
    },
    handleEdit(record, state) {
      this.$router.push({
        path: '/material/settlement/concrete/add',
        query: {
          id: record && record.id,
          state: state
        }
      })
    }
  }
}
</script>
